package com.weiyin.examination.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.weiyin.examination.R;
import com.weiyin.examination.adapter.CourseA7DialogAdapter;
import com.weiyin.examination.listener.DialogListResultInterface;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.view.GridLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 音译中
 *
 * @author njf
 * @date 2017/9/27
 */

public class CourseA7Dialog extends Dialog implements CourseA7DialogAdapter.OnItemClickInterface, View.OnClickListener {
    private Activity activity;
    private TextView textViewTitle, textViewOk;
    private RecyclerView recyclerView;
    private CourseA7DialogAdapter courseA7DialogAdapter;
    private List<String> data;
    private DialogListResultInterface dialogListResultInterface;
    private List<String> resultData;
    private String title;

    public void setDialogListResultInterface(DialogListResultInterface dialogListResultInterface) {
        this.dialogListResultInterface = dialogListResultInterface;
    }

    public CourseA7Dialog(@NonNull Context context) {
        super(context);
    }

    public CourseA7Dialog(@NonNull Activity activity, @StyleRes int themeResId, List<String> data, String title) {
        super(activity, themeResId);
        this.activity = activity;
        this.data = data;
        this.title = title;
    }

    protected CourseA7Dialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);
        LayoutHelper layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(findViewById(R.id.dialogLayout));
        initView();
    }

    private void initView() {
        resultData = new ArrayList<>();
        textViewTitle = (TextView) findViewById(R.id.dialogTextViewTitle);
        textViewOk = (TextView) findViewById(R.id.dialogTextViewOk);

        recyclerView = (RecyclerView) findViewById(R.id.dialogRecyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false));

        courseA7DialogAdapter = new CourseA7DialogAdapter(activity, R.layout.dialog_item_layout, data);
        recyclerView.setAdapter(courseA7DialogAdapter);
        courseA7DialogAdapter.setOnItemClickInterface(this);
        textViewTitle.setText(title);
        textViewOk.setOnClickListener(this);
    }

    @Override
    public void onItemClick(View view, int position) {
        LogUtil.i("a", data.get(position));
        if (!resultData.contains(data.get(position))) {
            LogUtil.i("a", "true");
            view.setSelected(true);
            resultData.add(data.get(position));
        } else {
            LogUtil.i("a", "false");
            view.setSelected(false);
            resultData.remove(data.get(position));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialogTextViewOk:
                if (dialogListResultInterface != null) {
                    dialogListResultInterface.result(resultData);
                }
                dismiss();
                break;
            default:
                break;
        }
    }
}
